﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Description;
using Server.Services;
using System.Xml.Serialization;
using System.IO;

namespace Server
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //var testc = new Server.ProjectHelpers.ProjectDecorator(5, "Primes");
            //Server.ProjectHelpers.Config p = new ProjectHelpers.Config() { Name = "Test", ClientLibFiles = new List<string>{"Client.dll", "tet.xtl"}, ConfigLibFile = "config.xml", MaxDaysOfCompiuting = 10, ServerLibFile = "Server.dll" };

            //XmlSerializer xs = new XmlSerializer(typeof(Server.ProjectHelpers.Config));
            //using (Stream s = File.Create("Project2.xml"))
            //{
            //    xs.Serialize(s, p);
            //}


            var test = new Controller();

            //test.AddProject("Primes");

            //Guid user = test.RegisterComputingUnit(new Processor { LogicalProcess = 2, Name = "test" }, "123.45.56.67");

            //var data = test.GetNextCompiutingData(user);
            //for(int i = 0; i < data.ParamitersCount; i++)
            //{
            //    var paramiter = test.GetParamiter(data.ProjectId, data.ParamitersIds[i], data.ParamitersIsDynamic[i]);
            //}

            Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
            ServiceHost selfHost = new CustomServiceHost(test, typeof(WebServices), baseAddress);
            selfHost.AddServiceEndpoint(
                    typeof(IWebServices),
                    new WSHttpBinding(),
                    "ServerWebService");
            // Step 4 of the hosting procedure: Enable metadata exchange.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            var behavior = selfHost.Description.Behaviors.Find<ServiceBehaviorAttribute>();
            behavior.IncludeExceptionDetailInFaults = true;

            // Step 5 of the hosting procedure: Start (and then stop) the service.
            selfHost.Open();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new GUIWindow(test));

            selfHost.Close();

        }
    }
}
